package com.redsoft.spirit.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.redsoft.spirit.entity.Department;
import com.redsoft.spirit.entity.JsonResult;
import com.redsoft.spirit.entity.TreeNode;
import com.redsoft.spirit.service.DeptService;

@RestController
public class DeptController {

	@Autowired
	private DeptService deptService;

	@RequestMapping(value = "/depts/getJsonTree", method = RequestMethod.GET)
	public String getJsonTree(String userId) {
		try {
			List<TreeNode> dept = deptService.getJsonTree(userId);
			JsonResult result = new JsonResult(dept);
			return result.success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 添加部门
	 * 
	 * <pre>
	 * 添加部门，页面传过来部门的实体类信息
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param dept
	 * @return
	 */
	@RequestMapping(value = "/depts", method = RequestMethod.POST)
	public String addDept(@RequestBody Department dept, String userId) {
		try {
			deptService.saveOrUpdate(dept, userId);

			List<TreeNode> depts = deptService.getJsonTree(userId);
			JsonResult result = new JsonResult(depts);

			return result.success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 查询部门
	 * 
	 * <pre>
	 * 根据传过来的id查询该部门以及该部门的上级部门名称
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param dept
	 * @return
	 */
	@RequestMapping(value = "/depts/{id}", method = RequestMethod.GET)
	public String getDept(@PathVariable String id) {
		try {
			Department dept = deptService.get(id);
			return new JsonResult(dept).success().toString();
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 获取所有公司list
	 * 
	 * <pre>
	 * 获取所有公司list
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@RequestMapping(value = "/**/depts", method = RequestMethod.GET)
	public String getDeptList() {
		try {
			List<Department> list = deptService.getDeptList();
			JSONObject json = new JSONObject();
			// 指定要返回的特定字段
			SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Department.class, "id", "name");

			// 返回信息,添加过滤器
			return json.toJSONString(new JsonResult(list).success(), filter);
		} catch (Exception e) {
			JsonResult result = new JsonResult(null, e.getMessage());
			return result.failure().toString();
		}
	}

	/**
	 * 根据ID删除
	 */
	@DeleteMapping("/**/depts/{id}")
	public String deleteDept(@PathVariable String id) {
		if (deptService.get(id) == null) {
			return new JsonResult(null, "该组织不存在").failure().toString();
		}
		if (deptService.get(id).getIsActive() == 0) {
			return new JsonResult(null, "该组织已停用，不能操作").failure().toString();
		}
		// 删除之前先从数据库查询是否还有下级部门，如果有，则返回错误，含有下级部门的不能直接停用。
		List<Department> list = deptService.getDeptByParentDeptId(id);
		if (list != null && list.size() != 0) {
			return new JsonResult(null, "该组织存在下级部门，不能停用").failure().toString();
		}
		
		deptService.deleteDept(id);
		return new JsonResult().success().toString();
	}
	
	/**
	 * 
	 * 已停用部门重新启用
	 * <pre>
	 * 已停用部门重新启用
	 * </pre>
	 * 
	 * @author lixiaodong
	 * @param id
	 * @return
	 */
	@PutMapping("/**/depts/{id}")
	public String recoverDept(@PathVariable String id) {
		if (deptService.get(id) == null) {
			return new JsonResult(null, "该组织不存在！").failure().toString();
		}
		if (deptService.get(id).getIsActive() == 1) {
			return new JsonResult(null, "该组织未停用，不用重新启用！").failure().toString();
		}
		
		deptService.recoverDept(id);
		return new JsonResult().success().toString();
	}
	
}